package com.wangp.mywrite.inwork.pattern._8._8_1_embedded;

import com.wangp.mywrite.inwork.pattern.InitData;

/***
 * @author wangpeng
 * @since 2022-03-04  10:04
 */
public class Embedded {

    public static void main(String[] args) {
        legalPhone();
//        backReferLegalPhone();
        postCode();
    }

    /**
     * 找出合法的北美手机号： （123）456-7890 和 123-456-7890
     */
    private static void legalPhone() {
        String phoneReg = "((\\(\\d{3}\\))|(\\d{3}))\\-\\d{3}\\-\\d{4}";
        // [123-456-7890, (123)-456-7890]
        System.out.println(InitData.filterByRegexPattern(InitData.NA_PHONE_LIST, phoneReg));
    }


    /**
     * 回溯引用
     */
    private static void backReferLegalPhone() {
        String phoneReg = "(\\()?\\d{3}((?(1)\\))|-)\\d{3}-\\d{4}";
        // java 中是不支持嵌入条件
        System.out.println(InitData.filterByRegexPattern(InitData.NA_PHONE_LIST, phoneReg));
    }

    /**
     * <p>获取邮编</p>
     * 正确格式是：一种是12345形式的ZIP格式，另一种是12345-6789形式的ZIP+4格式
     */
    private static void postCode() {
        String postCodeReg = "\\d{5}(\\-\\d{4})?";
        // [11111, 22222, 44444-4444]
        System.out.println(InitData.filterByRegexPattern(InitData.NA_POST_CODE_LIST,postCodeReg));
        // java 中是不支持嵌入条件
        String postCodeRegLookAround = "\\d{5}(?(?=-)-\\d{4})";
//        System.out.println(InitData.filterByRegexPattern(InitData.NA_POST_CODE_LIST,postCodeRegLookAround));
    }
}
